﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BlimpControlPanel
{
    class Utilities
    {
        public string ConvertMetersToFeet(string strMeters)
        {
            double feet = 0;
            double meters = 0;

            if (double.TryParse(strMeters, out meters))
                feet = meters * 3.2808399;

            return feet.ToString();
        }


        public string ConvertBytesToString(byte[] readBytes)
        {
            UTF7Encoding utf = new UTF7Encoding();
            string outString = utf.GetString(readBytes);
            outString += Environment.NewLine;
            return outString;
        }

        public string FormatTime(string strUTCTime)
        {
            string time = "";

            if (strUTCTime.ToCharArray().Length == 6)
            {
                time = strUTCTime.ToCharArray()[0].ToString() + strUTCTime.ToCharArray()[1].ToString() + ":";
                time += strUTCTime.ToCharArray()[2].ToString() + strUTCTime.ToCharArray()[3].ToString() + ":";
                time += strUTCTime.ToCharArray()[4].ToString() + strUTCTime.ToCharArray()[5].ToString();
            }
            else
                time = "00:00:00";

            return time;
        }

        public string FormatDate(string date)
        {
            string outDate = "";

            if (date.ToCharArray().Length == 6)
            {
                outDate = date.ToCharArray()[0].ToString() + date.ToCharArray()[1].ToString() + "/";
                outDate += date.ToCharArray()[2].ToString() + date.ToCharArray()[3].ToString() + "/";
                outDate += date.ToCharArray()[4].ToString() + date.ToCharArray()[5].ToString();
            }
            else
                outDate = "00/00/00";

            return outDate;

        }

        internal double [] ConverStringsToData(string[] dataLine)
        {
            double[] returnData = new double[dataLine.Length];
            int x = 0;
            double result;

            foreach (string str in dataLine)
            {
                if (double.TryParse(str, out result))
                    returnData[x++] = result;
                else
                    break;
            }

            return returnData;

        }

        public string ConvertLatitude(string latitude, string northSouth)
        {
            char[] charData = latitude.ToCharArray();
            string hours = charData[0].ToString() + charData[1].ToString();
            string minutes = "";

            for (int x = 2; x < charData.Length; x++)
                minutes += charData[x].ToString();

            return (northSouth == "S" ? "-" : "") + GetHoursString(hours, minutes);
        }

        public string ConvertLongitude(string logitude, string eastWest)
        {
            char[] charData = logitude.ToCharArray();
            string hours = charData[0].ToString() + charData[1].ToString() + charData[2].ToString();
            string minutes = "";

            for (int x = 3; x < charData.Length; x++)
                minutes += charData[x].ToString();

            return (eastWest == "W" ? "-" : "") + GetHoursString(hours, minutes);
        }

        private static string GetHoursString(string hours, string minutes)
        {
            double dbMinutes = 0;

            if (double.TryParse(minutes, out dbMinutes))
                dbMinutes = dbMinutes / 60;

            double dbHours = 0;

            if (double.TryParse(hours, out dbHours))
                dbHours = dbHours + dbMinutes;

            return dbHours.ToString();
        }

    }
}
